#pragma once
#include <ctype.h>
#pragma warning(push)
#pragma warning(disable:4996)
namespace Console{

class _cmd_console
{
public:
	static int _tprintf(__in_z __format_string const TCHAR* _Format, ...)
	{
		va_list vl;
		va_start(vl,_Format);
		return _vtprintf(_Format,vl);
	}

	static int printf(__in_z __format_string const CHAR* _Format, ...)
	{
		va_list vl;
		va_start(vl,_Format);
		return vprintf(_Format,vl);
	}

	static size_t print(const CHAR *_Str,size_t len=-1)
	{
		if(len==-1)
		{
			len=strlen(_Str);
		}
		return fwrite(_Str,len,1,stdout);
	}

	static void flush()
	{
		fflush(stdout);
	}

	static BOOL init()
	{
		BOOL bRet=AllocConsole();
		ATLASSERT(bRet);
		if(!bRet)
		{
			return bRet;
		}

		FILE *pf=_tfreopen(_T("CONIN$"),_T("r"),stdin);
		ATLASSERT(pf);
		if(!pf)
		{
			return FALSE;
		}
		_tfreopen(_T("CONOUT$"),_T("w"),stdout);
		ATLASSERT(pf);
		if(!pf)
		{
			return FALSE;
		}
		_tfreopen(_T("CONOUT$"),_T("w"),stderr);
		ATLASSERT(pf);
		if(!pf)
		{
			return FALSE;
		}
		return bRet;
	}
};
class _none_console
{
public:
	static int _tprintf(__in_z __format_string const TCHAR* _Format, ...){return 0;}
	static int printf(__in_z __format_string const CHAR* _Format, ...){return 0;}
	static BOOL init(){return TRUE;}
	static int print(const CHAR *_Str,size_t len=-1){return 0;}
};

//typedef isprint isprint;


typedef _cmd_console console;
//typedef _none_console console;
}
#pragma warning(pop)
using namespace Console;